package com.massfords.jaxb;

import java.io.File;
import java.io.FileReader;
import java.text.MessageFormat;

import org.apache.commons.io.IOUtils;
import static org.junit.Assert.*;

public class GeneratedCodeFixture {
    private String mExpectedPathPattern;
    private String mGeneratedPathPattern;
    
    public GeneratedCodeFixture(String aExpectedPathPattern, String aGeneratedPathPattern) {
        mExpectedPathPattern = aExpectedPathPattern;
        mGeneratedPathPattern = aGeneratedPathPattern;
    }
    
    public void assertAllFiles() throws Exception {
        String[] files = {
                "DepthFirstTraverserImpl",
                "BaseVisitor",
                "Traverser",
                "TraversingVisitor",
                "TraversingVisitorProgressMonitor",
                "Visitor",
                "Visitable"
        };
        
        for(String f : files) {
            assertClassGenerated(f);
            assertClassMatches(f);
        }
        
    }
    
    public void assertClassGenerated(String aClassName) {
        File file = new File(MessageFormat.format(mGeneratedPathPattern, aClassName));
        assertTrue("expected to find generated class:" + file, file.isFile());
    }
    
    public void assertClassMatches(String aClassName) throws Exception {
        String expected = MessageFormat.format(mExpectedPathPattern, aClassName);
        String actual = MessageFormat.format(mGeneratedPathPattern, aClassName);
        
        String expectedValue = IOUtils.toString(new FileReader(expected));
        String expectedValue_sansHeader = expectedValue.replaceAll("Generated on: [0-9]{4}\\.[0-9]{2}\\.[0-9]{2} at .+", "");
        
        String actualValue = IOUtils.toString(new FileReader(actual));
        String actualValue_sansHeader = actualValue.replaceAll("Generated on: [0-9]{4}\\.[0-9]{2}\\.[0-9]{2} at .+", "");
        
        assertEquals(expectedValue_sansHeader, actualValue_sansHeader);
    }
    
}
